home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / common2r / sendbug.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-08-27  |  8.6 KB  |  262 lines

  1. VERSION 5.00
  2. Begin VB.Form Form7 
  3.    Appearance      =   0  '2D
  4.    BackColor       =   &H00808080&
  5.    BorderStyle     =   0  'Kein
  6.    Caption         =   "Send Bug Report"
  7.    ClientHeight    =   3192
  8.    ClientLeft      =   0
  9.    ClientTop       =   0
  10.    ClientWidth     =   4680
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   3192
  15.    ScaleWidth      =   4680
  16.    StartUpPosition =   2  'Bildschirmmitte
  17.    Begin VB.TextBox DataArrival 
  18.       Appearance      =   0  '2D
  19.       Height          =   288
  20.       Left            =   960
  21.       TabIndex        =   3
  22.       Text            =   "Text1"
  23.       Top             =   2760
  24.       Visible         =   0   'False
  25.       Width           =   1212
  26.    End
  27.    Begin VB.CommandButton Exit 
  28.       Appearance      =   0  '2D
  29.       Caption         =   "Exit"
  30.       Height          =   255
  31.       Left            =   2280
  32.       TabIndex        =   2
  33.       Top             =   2880
  34.       Width           =   2295
  35.    End
  36.    Begin VB.CommandButton SendBugConnect 
  37.       Appearance      =   0  '2D
  38.       Caption         =   "Send Feedback"
  39.       Height          =   255
  40.       Left            =   120
  41.       TabIndex        =   1
  42.       Top             =   2880
  43.       Width           =   2055
  44.    End
  45.    Begin VB.TextBox Bugreporttxt 
  46.       Appearance      =   0  '2D
  47.       Height          =   2655
  48.       Left            =   120
  49.       MultiLine       =   -1  'True
  50.       TabIndex        =   0
  51.       Top             =   120
  52.       Width           =   4455
  53.    End
  54. Attribute VB_Name = "Form7"
  55. Attribute VB_GlobalNameSpace = False
  56. Attribute VB_Creatable = False
  57. Attribute VB_PredeclaredId = True
  58. Attribute VB_Exposed = False
  59. '*******************************************
  60. '*New Updates:
  61. '-Api Declarations! (needs no Winsock.ocx)
  62. '-Check if the Server respond with the right code
  63. '-Perform a better error check
  64. '-Use a better timeout routine to check if the Server
  65. 'times out
  66. '*******************************************
  67. Option Explicit
  68. Private bTrans As Boolean
  69. Private m_iStage As Integer
  70. Private Sock As Integer
  71. Private RC As Integer
  72. Private Bytes As Integer
  73. Private ResponseCode As Integer
  74. '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  75. 'CHANGE THIS SETTING LIKE YOU NEED IT
  76. '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  77. Private Const mailserver As String = "127.0.0.1"
  78. Private Const Tobox As String = "galgen@wtal.de"
  79. Private Const Frombox As String = "theuser@ofthisprogram.com"
  80. Private Const Subject As String = "User Feedback!"
  81. 'This is for the WaitforResponse Routine
  82. Private Declare Function timeGetTime Lib "winmm.dll" () As Long
  83. '***************************************************************
  84. 'Routine for connecting to the server
  85. '***************************************************************
  86. Sub SendBugConnect_Click()
  87. Dim StartupData As WSADataType
  88. Dim SocketBuffer As sockaddr
  89. Dim IpAddr As Long
  90. 'Ini the Winsocket
  91. RC = WSAStartup(&H101, StartupData)
  92. RC = WSAStartup(&H101, StartupData)
  93. 'Open a free Socket (with this source code you can also
  94. 'open several connections! Very useful for E-Mail Applications...)
  95. Sock = socket(AF_INET, SOCK_STREAM, 0)
  96. If Sock = SOCKET_ERROR Then
  97.     MsgBox "Cannot Create Socket."
  98.     Exit Sub
  99. End If
  100. 'Checks if the Hostname exists
  101. If RC = SOCKET_ERROR Then Exit Sub
  102. IpAddr = GetHostByNameAlias(mailserver)
  103. If IpAddr = -1 Then
  104.     MsgBox "Unknown Host: " + mailserver
  105.     Exit Sub
  106. End If
  107. 'This part is responsible for the connection
  108. SocketBuffer.sin_family = AF_INET
  109. SocketBuffer.sin_port = htons(25)
  110. SocketBuffer.sin_addr = IpAddr
  111. SocketBuffer.sin_zero = String$(8, 0)
  112. RC = connect(Sock, SocketBuffer, Len(SocketBuffer))
  113. 'If an error occured close the connection and
  114. 'send an error message to the text window
  115. If RC = SOCKET_ERROR Then
  116.         MsgBox "Cannot Connect to " + mailserver + _
  117.                             Chr$(13) + Chr$(10) + _
  118.                             GetWSAErrorString(WSAGetLastError())
  119.         closesocket Sock
  120.         RC = WSACleanup()
  121.         Exit Sub
  122. End If
  123. 'Select Receive Window
  124. RC = WSAAsyncSelect(Sock, DataArrival.hWnd, _
  125.                         ByVal &H202, ByVal FD_READ Or FD_CLOSE)
  126.     If RC = SOCKET_ERROR Then
  127.         MsgBox "Cannot Process Asynchronously."
  128.         closesocket Sock
  129.         RC = WSACleanup()
  130.         Exit Sub
  131.     End If
  132. bTrans = True
  133. m_iStage = 0
  134. DataArrival = ""
  135. ResponseCode = 220
  136. Call WaitForResponse
  137. End Sub
  138. '***************************************************************
  139. 'Transmit the E-Mail
  140. '***************************************************************
  141. Private Sub Transmit(iStage As Integer)
  142. Dim Helo As String, temp As String
  143. Dim pos As Integer
  144. Select Case m_iStage
  145. Case 1:
  146.     Helo = Frombox
  147.     pos = Len(Helo) - InStr(Helo, "@")
  148.     Helo = Right$(Helo, pos)
  149.     ResponseCode = 250
  150.     WinsockSendData ("HELO " & Helo & vbCrLf)
  151.     Call WaitForResponse
  152. Case 2:
  153.     ResponseCode = 250
  154.     WinsockSendData ("MAIL FROM: <" & Trim(Frombox) & ">" & vbCrLf)
  155.     Call WaitForResponse
  156. Case 3:
  157.     ResponseCode = 250
  158.     WinsockSendData ("RCPT TO: <" & Trim(Tobox) & ">" & vbCrLf)
  159.     Call WaitForResponse
  160. Case 4:
  161.     ResponseCode = 354
  162.     WinsockSendData ("DATA" & vbCrLf)
  163.     Call WaitForResponse
  164. Case 5:
  165. '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  166. 'If you want additional Headers like Date,Message-Id,...etc. !
  167. 'simply add them below                                      !
  168. '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  169.     temp = temp & "From: " & Frombox & vbNewLine
  170.     temp = temp & "To: " & Tobox & vbNewLine
  171.     temp = temp & "Subject: " & Subject & vbNewLine
  172.     'Header + Message
  173.     temp = temp & vbCrLf & Bugreporttxt.Text
  174.     'Send the Message & close connection
  175.     WinsockSendData (temp)
  176.     WinsockSendData (vbCrLf & "." & vbCrLf)
  177.     ResponseCode = 250
  178.     Call WaitForResponse
  179. Case 6:
  180.     WinsockSendData ("QUIT" & vbCrLf)
  181.     ResponseCode = 221
  182.     Call WaitForResponse
  183.     m_iStage = 0
  184.     bTrans = False
  185. End Select
  186. End Sub
  187. '***************************************************************
  188. 'Routine for arraving Data
  189. '***************************************************************
  190. Private Sub DataArrival_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  191. Dim MsgBuffer As String * 2048
  192. On Error Resume Next
  193.     If Sock > 0 Then
  194.         'Receive up to 2048 chars
  195.         Bytes = recv(Sock, ByVal MsgBuffer, 2048, 0)
  196.         
  197.         If Bytes > 0 Then
  198.             
  199.                 
  200.         If bTrans Then
  201.             If ResponseCode = Left(MsgBuffer, 3) Then
  202.             MsgBuffer = vbNullString
  203.             m_iStage = m_iStage + 1
  204.             Transmit m_iStage
  205.             Else
  206.                 closesocket (Sock)
  207.                 RC = WSACleanup()
  208.                 Sock = 0
  209.                 MsgBox "The Server responds with an unexpected Response Code!", vbOKOnly, "Error!"
  210.                 Exit Sub
  211.             End If
  212.         End If
  213.         ElseIf WSAGetLastError() <> WSAEWOULDBLOCK Then
  214.             closesocket (Sock)
  215.             RC = WSACleanup()
  216.             Sock = 0
  217.         End If
  218.     End If
  219. Refresh
  220. End Sub
  221. '**************************************************************
  222. ' Waits until time out, while waiting for response
  223. '**************************************************************
  224. Private Sub WaitForResponse()
  225. Dim Start As Integer
  226. Dim Tmr As Integer
  227. 'Works with an Api Declaration because it's more precious
  228. Start = timeGetTime
  229. While Bytes > 0
  230.     Tmr = timeGetTime - Start
  231.     DoEvents ' Let System keep checking for incoming response
  232.         
  233.     'Wait 50 seconds for response
  234.     If Tmr > 50000 Then
  235.         MsgBox "SMTP service error, timed out while waiting for response", 64, "Error!"
  236.         End
  237.     End If
  238. End Sub
  239. Private Sub WinsockSendData(DatatoSend As String)
  240. Dim RC As Integer
  241. Dim MsgBuffer As String * 2048
  242. MsgBuffer = DatatoSend
  243. RC = send(Sock, ByVal MsgBuffer, Len(DatatoSend), 0)
  244. 'If an error occurs send an error message and
  245. 'reset the winsock
  246. If RC = SOCKET_ERROR Then
  247.     MsgBox "Cannot Send Request." + _
  248.                             Chr$(13) + Chr$(10) + _
  249.                             Str$(WSAGetLastError()) + _
  250.                             GetWSAErrorString(WSAGetLastError())
  251.     closesocket Sock
  252.     RC = WSACleanup()
  253.     Exit Sub
  254. End If
  255. End Sub
  256. Private Sub Exit_Click()
  257. On Error Resume Next
  258.     closesocket Sock
  259.     RC = WSACleanup()
  260.     End
  261. End Sub
  262.